/**
 * EventPostType.js
 * @author Peter Edwards <tech@e-2.org>
 * @version 1.1
 * @uses AnyTime datepicker (http://www.ama3.com/anytime/)
 * @uses jQuery (http://www.jquery.com)
 * @package WordPress
 * @subpackage EventPostType_Plugin
 */


/* initialise admin pages */
jQuery(function($){
	/* hide dates filter from admin screen */
	$('select[name=m]').hide();
	if ($('#event_dates').length) {
		/* do datepickers */
    	var dateFormat = "%d/%m/%Y";
	    var timeFormat = "%h:%i%p";
	    var dateConverter = new AnyTime.Converter({format:dateFormat});
	    var timeConverter = new AnyTime.Converter({format:timeFormat});
	    $('input.datepicker').AnyTime_picker({format:dateFormat});
	    $('input.timepicker').AnyTime_picker({format:timeFormat});
	    $('input#event_dates_allday').click(function(){checkAllDay();});
	    $("#event_dates_duration").val("null");
	    function checkAllDay(){
    		if ($('#event_dates_allday:checked').length) {
	    		$('#event_dates_end_date').val("").attr("disabled", true);
		    	$('#event_dates_end_time').val("").attr("disabled", true);
    			$('#event_dates_start_time').val("").attr("disabled", true);
	    		$("#event_dates_duration_minutes").val("null");
		    	$("#event_dates_duration_minutes").hide();
			    $('#event_dates_start_time').hide();
			    $('#event_dates_end').hide();
		    } else {
			    $('#event_dates_end_date').removeAttr("disabled");
    			$('#event_dates_end_time').removeAttr("disabled");
	    		$('#event_dates_start_time').removeAttr("disabled");
			    $("#event_dates_duration_minutes").show();
    			$('#event_dates_start_time').show();
	    		$('#event_dates_end').show();
		    }
	    }
	    /* set end date as same as start date if empty */
	    $("#event_dates_start_date").change( function(e) {
		    if (!$('#event_dates_allday:checked').length && $("#event_dates_end_date").val() == "") {
                try {
        	        var fromTime = dateConverter.parse($("#event_dates_start_date").val()).getTime();
	                fromDay = new Date(fromTime);
        	        fromDay.setHours(2,0,0,0);
	                $("#event_dates_end_date").
    	                AnyTime_noPicker().
	                    removeAttr("disabled").
	                    AnyTime_picker({earliest:fromDay,format:dateFormat});
                } catch(e){};
	        }
        });
        $("#event_dates_duration_minutes").change(function(e) {
        	var duration = $("#event_dates_duration_minutes option:selected").val()
        	if (duration !== "null") {
    	    	if ($("#event_dates_start_date").val() !== "" && $("#event_dates_start_time").val() !== "") {
    		    	try {
    			    	var fromTime = timeConverter.parse($("#event_dates_start_time").val());
    				    fromTime.setMinutes(fromTime.getMinutes()+parseInt(duration));
                	    $("#event_dates_end_time").val(timeConverter.format(fromTime));
                	    $("#event_dates_end_date").val($("#event_dates_start_date").val());
	                } catch(e){};
    		    }
    	    }
        });
        /* set the duration when loading */
        if ($("#event_dates_start_time").val() !== "" && $("#event_dates_end_time").val() !== "") {
	    	try {
		    	var fromTime = timeConverter.parse($("#event_dates_start_time").val());
		    	var toTime = timeConverter.parse($("#event_dates_end_time").val());
		    	$("#event_dates_duration_minutes").val((toTime - fromTime)/60000);
            } catch(e){};
        }
	    checkAllDay();
	}
});